package com.ybkj.daijia.server.controller;

import com.alibaba.fastjson.JSONObject;
import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.SmsTempletContent;
import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.server.event.model.OperationLogEvent;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.SmsRecord;
import com.ybkj.daijia.server.model.SmsTemplet;
import com.ybkj.daijia.server.model.SmsTemplet.SmsType;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverRebateService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.MemberRebateService;
import com.ybkj.daijia.server.service.SmsService;
import com.ybkj.daijia.server.service.SmsTempletService;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

@Controller
@RequestMapping(value = "sms")
public class SmsController {

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DriverService driverService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private SmsService smsService;

    @Autowired
    private SmsTempletService smsTempletService;

    @Autowired
    private MemberRebateService memberRebateService;

    @Autowired
    private DriverRebateService driverRebateService;


    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;

    @RequestMapping(value = "list")
    public String findAll(Model model) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("sms")) {
            try {

                Page<SmsRecord> page = smsService.queryByParam(null, null, null, null);
                model.addAttribute("page", page);
                //短信条数
                Long count = smsService.querySmsAppBalance();

                model.addAttribute("count", count);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "sms/list";
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "editSms", method = RequestMethod.GET)
    public ModelAndView add() {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        if (subject.isPermitted("sms")) {
            ModelAndView mv = new ModelAndView();
            Company main = companyService.getMain();

            if (!principal.getCompanyId().equals(main.getId())) {
                List<Company> list = new ArrayList<Company>();
                list.add(companyService.findOne(principal.getCompanyId()));
                mv.addObject("companys", list);
            } else {
                List<Company> list = companyService.findAll();
                mv.addObject("companys", list);
            }

            mv.addObject("setting", settingUtils.get());
            Setting setting = settingUtils.get();
            mv.addObject("isSmsMainCompany", setting.isSmsMainCompany());
            mv.addObject("mainCompanyId", main.getId());
            mv.setViewName("sms/editSms");
            return mv;
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "setting/{id}", method = RequestMethod.GET)
    public ModelAndView setting(@PathVariable("id") Long id) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("sms")) {
            ModelAndView mv = new ModelAndView();
            Company local = companyService.findOne(id);

            mv.setViewName("sms/setting");
            mv.addObject("company", local);
            mv.addObject("setting", settingUtils.get());

//			短信
//			List<SmsTemplet> list=smsService.findByCompanyId(local.getId());
//			2019-10-23 改
            List<SmsTemplet> list = smsTempletService.listByCompany(local.getId());
            for (SmsTemplet smsTemplet : list) {
                String smsKey = smsTemplet.getSmsType().toString();
                mv.addObject("sms_" + smsKey, smsTemplet);
            }

            mv.addObject("username", principal.getUsername());

            return mv;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "saveSmsMainCompany", method = RequestMethod.POST)
    public void saveSmsMainCompany(boolean smsMainCompany) {
        Setting oldsetting = settingUtils.get();
        oldsetting.setSmsMainCompany(smsMainCompany);
        settingUtils.set(oldsetting);
    }


    @RequestMapping(value = "saveSetting", method = RequestMethod.POST)
    public String saveSetting(Model model, SmsTempletContent smsTempletContent, Long companyId,
        String signName, HttpServletRequest req) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("company")) {

            Company local = companyService.findOne(companyId);
            if (principal.getUsername().equals("shine")) {
                local.setSignName(signName);
                String basePath = getBasePath(req);
                companyService.update(local, basePath);
            }

            if (subject.isPermitted("smstemplet")) {
                SmsTemplet sms_driverCreate = smsService
                    .findByAlias(companyId, SmsType.driverCreate);
                if (null == sms_driverCreate) {
                    sms_driverCreate = new SmsTemplet();
                    sms_driverCreate.setCompanyId(companyId);
                    sms_driverCreate.setContent(smsTempletContent.getDriverCreate());
                    sms_driverCreate.setSmsType(SmsTemplet.SmsType.driverCreate);
                    smsService.insertSmsTemplate("新建服务人员发送密码", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_driverCreate);//调用短信系统的保存短信模板
                } else {
                    if (!sms_driverCreate.getContent()
                        .equals(smsTempletContent.getDriverCreate())) {
                        sms_driverCreate.setContent(smsTempletContent.getDriverCreate());
                        smsService.update(sms_driverCreate);//调用短信系统的修改短信模板
                    }
                }

                SmsTemplet sms_driverChangePassword = smsService
                    .findByAlias(companyId, SmsType.driverChangePassword);
                if (null == sms_driverChangePassword) {
                    sms_driverChangePassword = new SmsTemplet();
                    sms_driverChangePassword.setCompanyId(companyId);
                    sms_driverChangePassword
                        .setContent(smsTempletContent.getDriverChangePassword());
                    sms_driverChangePassword.setSmsType(SmsTemplet.SmsType.driverChangePassword);
                    smsService.insertSmsTemplate("修改服务人员密码", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_driverChangePassword);
                } else {
                    if (!sms_driverChangePassword.getContent()
                        .equals(smsTempletContent.getDriverChangePassword())) {
                        sms_driverChangePassword
                            .setContent(smsTempletContent.getDriverChangePassword());
                        smsService.update(sms_driverChangePassword);
                    }
                }

                SmsTemplet sms_driverMoney = smsService.findByAlias(companyId, SmsType.driverMoney);
                if (null == sms_driverMoney) {
                    sms_driverMoney = new SmsTemplet();
                    sms_driverMoney.setCompanyId(companyId);
                    sms_driverMoney.setContent(smsTempletContent.getDriverMoney());
                    sms_driverMoney.setSmsType(SmsTemplet.SmsType.driverMoney);
                    smsService.insertSmsTemplate("服务人员充值", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_driverMoney);
                } else {
                    if (!sms_driverMoney.getContent().equals(smsTempletContent.getDriverMoney())) {
                        sms_driverMoney.setContent(smsTempletContent.getDriverMoney());
                        smsService.update(sms_driverMoney);
                    }
                }

                SmsTemplet sms_memberCode = smsService.findByAlias(companyId, SmsType.memberCode);
                if (null == sms_memberCode) {
                    sms_memberCode = new SmsTemplet();
                    sms_memberCode.setCompanyId(companyId);
                    sms_memberCode.setContent(smsTempletContent.getMemberCode());
                    sms_memberCode.setSmsType(SmsTemplet.SmsType.memberCode);
                    smsService.insertSmsTemplate("客户端登陆验证码", SmsTemplet.SMSTEMPLATE_TYPE_CODE,
                        sms_memberCode);
                } else {
                    if (!sms_memberCode.getContent().equals(smsTempletContent.getMemberCode())) {
                        sms_memberCode.setContent(smsTempletContent.getMemberCode());
                        smsService.update(sms_memberCode);
                    }
                }

                SmsTemplet sms_driverCode = smsService.findByAlias(companyId, SmsType.driverCode);
                if (null == sms_driverCode) {
                    sms_driverCode = new SmsTemplet();
                    sms_driverCode.setCompanyId(companyId);
                    sms_driverCode.setContent(smsTempletContent.getDriverCode());
                    sms_driverCode.setSmsType(SmsTemplet.SmsType.driverCode);
                    smsService.insertSmsTemplate("司机端登陆验证码", SmsTemplet.SMSTEMPLATE_TYPE_CODE,
                        sms_driverCode);
                } else {
                    if (!sms_driverCode.getContent().equals(smsTempletContent.getDriverCode())) {
                        sms_driverCode.setContent(smsTempletContent.getDriverCode());
                        smsService.update(sms_driverCode);
                    }
                }

                SmsTemplet sms_newOrder = smsService.findByAlias(companyId, SmsType.newOrder);
                if (null == sms_newOrder) {
                    sms_newOrder = new SmsTemplet();
                    sms_newOrder.setCompanyId(companyId);
                    sms_newOrder.setContent(smsTempletContent.getNewOrder());
                    sms_newOrder.setSmsType(SmsTemplet.SmsType.newOrder);
                    smsService.insertSmsTemplate("派给服务人员新工单", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_newOrder);
                } else {
                    if (!sms_newOrder.getContent().equals(smsTempletContent.getNewOrder())) {
                        sms_newOrder.setContent(smsTempletContent.getNewOrder());
                        smsService.update(sms_newOrder);
                    }
                }

                SmsTemplet sms_djxNotice = smsService.findByAlias(companyId, SmsType.djxNotice);
                if (null == sms_djxNotice) {
                    sms_djxNotice = new SmsTemplet();
                    sms_djxNotice.setCompanyId(companyId);
                    sms_djxNotice.setContent(smsTempletContent.getDjxNotice());
                    sms_djxNotice.setSmsType(SmsTemplet.SmsType.djxNotice);
                    smsService.insertSmsTemplate("代驾险通知", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_djxNotice);
                } else {
                    if (!sms_djxNotice.getContent().equals(smsTempletContent.getDjxNotice())) {
                        sms_djxNotice.setContent(smsTempletContent.getDjxNotice());
                        smsService.update(sms_djxNotice);
                    }
                }

                SmsTemplet sms_ywxNotice = smsService.findByAlias(companyId, SmsType.ywxNotice);
                if (null == sms_ywxNotice) {
                    sms_ywxNotice = new SmsTemplet();
                    sms_ywxNotice.setCompanyId(companyId);
                    sms_ywxNotice.setContent(smsTempletContent.getYwxNotice());
                    sms_ywxNotice.setSmsType(SmsTemplet.SmsType.ywxNotice);
                    smsService.insertSmsTemplate("意外险通知", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_ywxNotice);
                } else {
                    if (!sms_ywxNotice.getContent().equals(smsTempletContent.getYwxNotice())) {
                        sms_ywxNotice.setContent(smsTempletContent.getYwxNotice());
                        smsService.update(sms_ywxNotice);
                    }
                }

                SmsTemplet sms_cancelOrder = smsService.findByAlias(companyId, SmsType.cancelOrder);
                if (null == sms_cancelOrder) {
                    sms_cancelOrder = new SmsTemplet();
                    sms_cancelOrder.setCompanyId(companyId);
                    sms_cancelOrder.setContent(smsTempletContent.getCancelOrder());
                    sms_cancelOrder.setSmsType(SmsTemplet.SmsType.cancelOrder);
                    smsService.insertSmsTemplate("服务人员被销单", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_cancelOrder);
                } else {
                    if (!sms_cancelOrder.getContent().equals(smsTempletContent.getCancelOrder())) {
                        sms_cancelOrder.setContent(smsTempletContent.getCancelOrder());
                        smsService.update(sms_cancelOrder);
                    }
                }

                SmsTemplet sms_revokeOrder = smsService.findByAlias(companyId, SmsType.revokeOrder);
                if (null == sms_revokeOrder) {
                    sms_revokeOrder = new SmsTemplet();
                    sms_revokeOrder.setCompanyId(companyId);
                    sms_revokeOrder.setContent(smsTempletContent.getRevokeOrder());
                    sms_revokeOrder.setSmsType(SmsTemplet.SmsType.revokeOrder);
                    smsService.insertSmsTemplate("服务人员订单被收回", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_revokeOrder);
                } else {
                    if (!sms_revokeOrder.getContent().equals(smsTempletContent.getRevokeOrder())) {
                        sms_revokeOrder.setContent(smsTempletContent.getRevokeOrder());
                        smsService.update(sms_revokeOrder);
                    }
                }

                SmsTemplet sms_driverAccept = smsService
                    .findByAlias(companyId, SmsType.driverAccept);
                if (null == sms_driverAccept) {
                    sms_driverAccept = new SmsTemplet();
                    sms_driverAccept.setCompanyId(companyId);
                    sms_driverAccept.setContent(smsTempletContent.getDriverAccept());
                    sms_driverAccept.setSmsType(SmsTemplet.SmsType.driverAccept);
                    smsService.insertSmsTemplate("服务人员接单（发给客户）", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_driverAccept);
                } else {
                    if (!sms_driverAccept.getContent()
                        .equals(smsTempletContent.getDriverAccept())) {
                        sms_driverAccept.setContent(smsTempletContent.getDriverAccept());
                        smsService.update(sms_driverAccept);
                    }
                }

                SmsTemplet sms_arriveFromPlace = smsService
                    .findByAlias(companyId, SmsType.arriveFromPlace);
                if (null == sms_arriveFromPlace) {
                    sms_arriveFromPlace = new SmsTemplet();
                    sms_arriveFromPlace.setCompanyId(companyId);
                    sms_arriveFromPlace.setContent(smsTempletContent.getArriveFromPlace());
                    sms_arriveFromPlace.setSmsType(SmsTemplet.SmsType.arriveFromPlace);
                    smsService.insertSmsTemplate("到达预约地（发给客户）", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_arriveFromPlace);
                } else {
                    if (!sms_arriveFromPlace.getContent()
                        .equals(smsTempletContent.getArriveFromPlace())) {
                        sms_arriveFromPlace.setContent(smsTempletContent.getArriveFromPlace());
                        smsService.update(sms_arriveFromPlace);
                    }
                }

                SmsTemplet sms_orderFinished = smsService
                    .findByAlias(companyId, SmsType.orderFinished);
                if (null == sms_orderFinished) {
                    sms_orderFinished = new SmsTemplet();
                    sms_orderFinished.setCompanyId(companyId);
                    sms_orderFinished.setContent(smsTempletContent.getOrderFinished());
                    sms_orderFinished.setSmsType(SmsTemplet.SmsType.orderFinished);
                    smsService.insertSmsTemplate("订单完成（发给客户）", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_orderFinished);
                } else {
                    if (!sms_orderFinished.getContent()
                        .equals(smsTempletContent.getOrderFinished())) {
                        sms_orderFinished.setContent(smsTempletContent.getOrderFinished());
                        smsService.update(sms_orderFinished);
                    }
                }

                SmsTemplet sms_modifyCash = smsService.findByAlias(companyId, SmsType.modifyCash);
                if (null == sms_modifyCash) {
                    sms_modifyCash = new SmsTemplet();
                    sms_modifyCash.setCompanyId(companyId);
                    sms_modifyCash.setContent(smsTempletContent.getModifyCash());
                    sms_modifyCash.setSmsType(SmsTemplet.SmsType.modifyCash);
                    smsService.insertSmsTemplate("修改订单金额（发给客户）", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_modifyCash);
                } else {
                    if (!sms_modifyCash.getContent().equals(smsTempletContent.getModifyCash())) {
                        sms_modifyCash.setContent(smsTempletContent.getModifyCash());
                        smsService.update(sms_modifyCash);
                    }
                }

                SmsTemplet sms_yupaiOrder = smsService.findByAlias(companyId, SmsType.yupaiOrder);
                if (null == sms_yupaiOrder) {
                    sms_yupaiOrder = new SmsTemplet();
                    sms_yupaiOrder.setCompanyId(companyId);
                    sms_yupaiOrder.setContent(smsTempletContent.getYupaiOrder());
                    sms_yupaiOrder.setSmsType(SmsTemplet.SmsType.yupaiOrder);
                    smsService.insertSmsTemplate("给服务人员预派单", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_yupaiOrder);
                } else {
                    if (!sms_yupaiOrder.getContent().equals(smsTempletContent.getYupaiOrder())) {
                        sms_yupaiOrder.setContent(smsTempletContent.getYupaiOrder());
                        smsService.update(sms_yupaiOrder);
                    }
                }

                SmsTemplet sms_memberRechargeRecord = smsService
                    .findByAlias(companyId, SmsType.memberRechargeRecord);
                if (null == sms_memberRechargeRecord) {
                    sms_memberRechargeRecord = new SmsTemplet();
                    sms_memberRechargeRecord.setCompanyId(companyId);
                    sms_memberRechargeRecord
                        .setContent(smsTempletContent.getMemberRechargeRecord());
                    sms_memberRechargeRecord.setSmsType(SmsTemplet.SmsType.memberRechargeRecord);
                    smsService.insertSmsTemplate("客户存入", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_memberRechargeRecord);
                } else {
                    if (!sms_memberRechargeRecord.getContent()
                        .equals(smsTempletContent.getMemberRechargeRecord())) {
                        sms_memberRechargeRecord
                            .setContent(smsTempletContent.getMemberRechargeRecord());
                        smsService.update(sms_memberRechargeRecord);
                    }
                }

                SmsTemplet sms_memberConsumedRecord = smsService
                    .findByAlias(companyId, SmsType.memberConsumedRecord);
                if (null == sms_memberConsumedRecord) {
                    sms_memberConsumedRecord = new SmsTemplet();
                    sms_memberConsumedRecord.setCompanyId(companyId);
                    sms_memberConsumedRecord
                        .setContent(smsTempletContent.getMemberConsumedRecord());
                    sms_memberConsumedRecord.setSmsType(SmsTemplet.SmsType.memberConsumedRecord);
                    smsService.insertSmsTemplate("客户支出", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_memberConsumedRecord);
                } else {
                    if (!sms_memberConsumedRecord.getContent()
                        .equals(smsTempletContent.getMemberConsumedRecord())) {
                        sms_memberConsumedRecord
                            .setContent(smsTempletContent.getMemberConsumedRecord());
                        smsService.update(sms_memberConsumedRecord);
                    }
                }

                SmsTemplet sms_activityCoupon = smsService
                    .findByAlias(companyId, SmsType.activityCoupon);
                if (null == sms_activityCoupon) {
                    sms_activityCoupon = new SmsTemplet();
                    sms_activityCoupon.setCompanyId(companyId);
                    sms_activityCoupon.setContent(smsTempletContent.getActivityCoupon());
                    sms_activityCoupon.setSmsType(SmsTemplet.SmsType.activityCoupon);
                    smsService.insertSmsTemplate("活动返劵", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_activityCoupon);
                } else {
                    if (!sms_activityCoupon.getContent()
                        .equals(smsTempletContent.getActivityCoupon())) {
                        sms_activityCoupon.setContent(smsTempletContent.getActivityCoupon());
                        smsService.update(sms_activityCoupon);
                    }
                }

                SmsTemplet sms_activity = smsService.findByAlias(companyId, SmsType.activity);
                if (null == sms_activity) {
                    sms_activity = new SmsTemplet();
                    sms_activity.setCompanyId(companyId);
                    sms_activity.setContent(smsTempletContent.getActivity());
                    sms_activity.setSmsType(SmsTemplet.SmsType.activity);
                    smsService.insertSmsTemplate("活动返现", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_activity);
                } else {
                    if (!sms_activity.getContent().equals(smsTempletContent.getActivity())) {
                        sms_activity.setContent(smsTempletContent.getActivity());
                        smsService.update(sms_activity);
                    }
                }

                SmsTemplet sms_recharge = smsService.findByAlias(companyId, SmsType.recharge);
                if (null == sms_recharge) {
                    sms_recharge = new SmsTemplet();
                    sms_recharge.setCompanyId(companyId);
                    sms_recharge.setContent(smsTempletContent.getRecharge());
                    sms_recharge.setSmsType(SmsTemplet.SmsType.recharge);
                    smsService.insertSmsTemplate("客户/服务人员充值", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_recharge);
                } else {
                    if (!sms_recharge.getContent().equals(smsTempletContent.getRecharge())) {
                        sms_recharge.setContent(smsTempletContent.getRecharge());
                        smsService.update(sms_recharge);
                    }
                }

                SmsTemplet sms_baoxianMember = smsService
                    .findByAlias(companyId, SmsType.baoxianMember);
                if (null == sms_baoxianMember) {
                    sms_baoxianMember = new SmsTemplet();
                    sms_baoxianMember.setCompanyId(companyId);
                    sms_baoxianMember.setContent(smsTempletContent.getBaoxianMember());
                    sms_baoxianMember.setSmsType(SmsTemplet.SmsType.baoxianMember);
                    smsService.insertSmsTemplate("购买代驾险（发给客户）", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_baoxianMember);
                } else {
                    if (!sms_baoxianMember.getContent()
                        .equals(smsTempletContent.getBaoxianMember())) {
                        sms_baoxianMember.setContent(smsTempletContent.getBaoxianMember());
                        smsService.update(sms_baoxianMember);
                    }
                }

                SmsTemplet sms_baoxianDriver = smsService
                    .findByAlias(companyId, SmsType.baoxianDriver);
                if (null == sms_baoxianDriver) {
                    sms_baoxianDriver = new SmsTemplet();
                    sms_baoxianDriver.setCompanyId(companyId);
                    sms_baoxianDriver.setContent(smsTempletContent.getBaoxianDriver());
                    sms_baoxianDriver.setSmsType(SmsTemplet.SmsType.baoxianDriver);
                    smsService
                        .insertSmsTemplate("购买代驾险（发给服务人员）", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                            sms_baoxianDriver);
                } else {
                    if (!sms_baoxianDriver.getContent()
                        .equals(smsTempletContent.getBaoxianDriver())) {
                        sms_baoxianDriver.setContent(smsTempletContent.getBaoxianDriver());
                        smsService.update(sms_baoxianDriver);
                    }
                }
                /*跑腿短信模板*/
                SmsTemplet sms_paiPaotuiOrder = smsService
                    .findByAlias(companyId, SmsType.paiPaotuiOrder);
                if (null == sms_paiPaotuiOrder) {
                    sms_paiPaotuiOrder = new SmsTemplet();
                    sms_paiPaotuiOrder.setCompanyId(companyId);
                    sms_paiPaotuiOrder.setContent(smsTempletContent.getPaiPaotuiOrder());
                    sms_paiPaotuiOrder.setSmsType(SmsTemplet.SmsType.paiPaotuiOrder);
                    smsService.insertSmsTemplate("派给服务人员新工单", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_paiPaotuiOrder);
                } else {
                    if (!sms_paiPaotuiOrder.getContent()
                        .equals(smsTempletContent.getPaiPaotuiOrder())) {
                        sms_paiPaotuiOrder.setContent(smsTempletContent.getPaiPaotuiOrder());
                        smsService.update(sms_paiPaotuiOrder);
                    }
                }

                SmsTemplet sms_acceptPaotuiOrder = smsService
                    .findByAlias(companyId, SmsType.acceptPaotuiOrder);
                if (null == sms_acceptPaotuiOrder) {
                    sms_acceptPaotuiOrder = new SmsTemplet();
                    sms_acceptPaotuiOrder.setCompanyId(companyId);
                    sms_acceptPaotuiOrder.setContent(smsTempletContent.getAcceptPaotuiOrder());
                    sms_acceptPaotuiOrder.setSmsType(SmsTemplet.SmsType.acceptPaotuiOrder);
                    smsService.insertSmsTemplate("服务人员接单（发给客户）", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_acceptPaotuiOrder);
                } else {
                    if (!sms_acceptPaotuiOrder.getContent()
                        .equals(smsTempletContent.getAcceptPaotuiOrder())) {
                        sms_acceptPaotuiOrder.setContent(smsTempletContent.getAcceptPaotuiOrder());
                        smsService.update(sms_acceptPaotuiOrder);
                    }
                }

                SmsTemplet sms_cancelPaotuiOrder = smsService
                    .findByAlias(companyId, SmsType.cancelPaotuiOrder);
                if (null == sms_cancelPaotuiOrder) {
                    sms_cancelPaotuiOrder = new SmsTemplet();
                    sms_cancelPaotuiOrder.setCompanyId(companyId);
                    sms_cancelPaotuiOrder.setContent(smsTempletContent.getCancelPaotuiOrder());
                    sms_cancelPaotuiOrder.setSmsType(SmsTemplet.SmsType.cancelPaotuiOrder);
                    smsService.insertSmsTemplate("服务人员销单（发给客户）", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_cancelPaotuiOrder);
                } else {
                    if (!sms_cancelPaotuiOrder.getContent()
                        .equals(smsTempletContent.getCancelPaotuiOrder())) {
                        sms_cancelPaotuiOrder.setContent(smsTempletContent.getCancelPaotuiOrder());
                        smsService.update(sms_cancelPaotuiOrder);
                    }
                }

                SmsTemplet sms_revokePaotuiOrder = smsService
                    .findByAlias(companyId, SmsType.revokePaotuiOrder);
                if (null == sms_revokePaotuiOrder) {
                    sms_revokePaotuiOrder = new SmsTemplet();
                    sms_revokePaotuiOrder.setCompanyId(companyId);
                    sms_revokePaotuiOrder.setContent(smsTempletContent.getRevokePaotuiOrder());
                    sms_revokePaotuiOrder.setSmsType(SmsTemplet.SmsType.revokePaotuiOrder);
                    smsService.insertSmsTemplate("跑腿单收回", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_revokePaotuiOrder);
                } else {
                    if (!sms_revokePaotuiOrder.getContent()
                        .equals(smsTempletContent.getRevokePaotuiOrder())) {
                        sms_revokePaotuiOrder.setContent(smsTempletContent.getRevokePaotuiOrder());
                        smsService.update(sms_revokePaotuiOrder);
                    }
                }

                SmsTemplet sms_finishPaotuiOrder = smsService
                    .findByAlias(companyId, SmsType.finishPaotuiOrder);
                if (null == sms_finishPaotuiOrder) {
                    sms_finishPaotuiOrder = new SmsTemplet();
                    sms_finishPaotuiOrder.setCompanyId(companyId);
                    sms_finishPaotuiOrder.setContent(smsTempletContent.getFinishPaotuiOrder());
                    sms_finishPaotuiOrder.setSmsType(SmsTemplet.SmsType.finishPaotuiOrder);
                    smsService.insertSmsTemplate("订单完成（发给客户）", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_finishPaotuiOrder);
                } else {
                    if (!sms_finishPaotuiOrder.getContent()
                        .equals(smsTempletContent.getFinishPaotuiOrder())) {
                        sms_finishPaotuiOrder.setContent(smsTempletContent.getFinishPaotuiOrder());
                        smsService.update(sms_finishPaotuiOrder);
                    }
                }

                SmsTemplet sms_modifyPaotuiCash = smsService
                    .findByAlias(companyId, SmsType.modifyPaotuiCash);
                if (null == sms_modifyPaotuiCash) {
                    sms_modifyPaotuiCash = new SmsTemplet();
                    sms_modifyPaotuiCash.setCompanyId(companyId);
                    sms_modifyPaotuiCash.setContent(smsTempletContent.getModifyPaotuiCash());
                    sms_modifyPaotuiCash.setSmsType(SmsTemplet.SmsType.modifyPaotuiCash);
                    smsService.insertSmsTemplate("跑腿单修改订单金额", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_modifyPaotuiCash);
                } else {
                    if (!sms_modifyPaotuiCash.getContent()
                        .equals(smsTempletContent.getModifyPaotuiCash())) {
                        sms_modifyPaotuiCash.setContent(smsTempletContent.getModifyPaotuiCash());
                        smsService.update(sms_modifyPaotuiCash);
                    }
                }

                SmsTemplet sms_yupaiPaotuiOrder = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.yupaiPaotuiOrder);
                if (null == sms_yupaiPaotuiOrder) {
                    sms_yupaiPaotuiOrder = new SmsTemplet();
                    sms_yupaiPaotuiOrder.setCompanyId(companyId);
                    sms_yupaiPaotuiOrder.setContent(smsTempletContent.getYupaiPaotuiOrder());
                    sms_yupaiPaotuiOrder.setSmsType(SmsTemplet.SmsType.yupaiPaotuiOrder);
                    smsService.insertSmsTemplate("跑腿单预派单", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_yupaiPaotuiOrder);
                } else {
                    if (!sms_yupaiPaotuiOrder.getContent()
                        .equals(smsTempletContent.getYupaiPaotuiOrder())) {
                        sms_yupaiPaotuiOrder.setContent(smsTempletContent.getYupaiPaotuiOrder());
                        smsService.update(sms_yupaiPaotuiOrder);
                    }
                }

                //专车相关
                SmsTemplet sms_paiZhuancheOrder = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.paiZhuancheOrder);
                if (null == sms_paiZhuancheOrder) {
                    sms_paiZhuancheOrder = new SmsTemplet();
                    sms_paiZhuancheOrder.setCompanyId(companyId);
                    sms_paiZhuancheOrder.setContent(smsTempletContent.getPaiZhuancheOrder());
                    sms_paiZhuancheOrder.setSmsType(SmsTemplet.SmsType.paiZhuancheOrder);
                    smsService.insertSmsTemplate("派给服务人员新工单", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_paiZhuancheOrder);
                } else {
                    if (!sms_paiZhuancheOrder.getContent()
                        .equals(smsTempletContent.getPaiZhuancheOrder())) {
                        sms_paiZhuancheOrder.setContent(smsTempletContent.getPaiZhuancheOrder());
                        smsService.update(sms_paiZhuancheOrder);
                    }
                }

                SmsTemplet sms_acceptZhuancheOrder = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.acceptZhuancheOrder);
                if (null == sms_acceptZhuancheOrder) {
                    sms_acceptZhuancheOrder = new SmsTemplet();
                    sms_acceptZhuancheOrder.setCompanyId(companyId);
                    sms_acceptZhuancheOrder.setContent(smsTempletContent.getAcceptZhuancheOrder());
                    sms_acceptZhuancheOrder.setSmsType(SmsTemplet.SmsType.acceptZhuancheOrder);
                    smsService.insertSmsTemplate("服务人员接单（发给客户）", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_acceptZhuancheOrder);
                } else {
                    if (!sms_acceptZhuancheOrder.getContent()
                        .equals(smsTempletContent.getAcceptZhuancheOrder())) {
                        sms_acceptZhuancheOrder
                            .setContent(smsTempletContent.getAcceptZhuancheOrder());
                        smsService.update(sms_acceptZhuancheOrder);
                    }
                }

                SmsTemplet sms_cancelZhuancheOrder = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.cancelZhuancheOrder);
                if (null == sms_cancelZhuancheOrder) {
                    sms_cancelZhuancheOrder = new SmsTemplet();
                    sms_cancelZhuancheOrder.setCompanyId(companyId);
                    sms_cancelZhuancheOrder.setContent(smsTempletContent.getCancelZhuancheOrder());
                    sms_cancelZhuancheOrder.setSmsType(SmsTemplet.SmsType.cancelZhuancheOrder);
                    smsService.insertSmsTemplate("服务人员销单（发给客户）", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_cancelZhuancheOrder);
                } else {
                    if (!sms_cancelZhuancheOrder.getContent()
                        .equals(smsTempletContent.getCancelZhuancheOrder())) {
                        sms_cancelZhuancheOrder
                            .setContent(smsTempletContent.getCancelZhuancheOrder());
                        smsService.update(sms_cancelZhuancheOrder);
                    }
                }

                SmsTemplet sms_revokeZhuancheOrder = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.revokeZhuancheOrder);
                if (null == sms_revokeZhuancheOrder) {
                    sms_revokeZhuancheOrder = new SmsTemplet();
                    sms_revokeZhuancheOrder.setCompanyId(companyId);
                    sms_revokeZhuancheOrder.setContent(smsTempletContent.getRevokeZhuancheOrder());
                    sms_revokeZhuancheOrder.setSmsType(SmsTemplet.SmsType.revokeZhuancheOrder);
                    smsService.insertSmsTemplate("专车单收回", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_revokeZhuancheOrder);
                } else {
                    if (!sms_revokeZhuancheOrder.getContent()
                        .equals(smsTempletContent.getRevokeZhuancheOrder())) {
                        sms_revokeZhuancheOrder
                            .setContent(smsTempletContent.getRevokeZhuancheOrder());
                        smsService.update(sms_revokeZhuancheOrder);
                    }
                }

                SmsTemplet sms_arriveFromZhuanchePlace = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.arriveFromZhuanchePlace);
                if (null == sms_arriveFromZhuanchePlace) {
                    sms_arriveFromZhuanchePlace = new SmsTemplet();
                    sms_arriveFromZhuanchePlace.setCompanyId(companyId);
                    sms_arriveFromZhuanchePlace
                        .setContent(smsTempletContent.getArriveFromZhuanchePlace());
                    sms_arriveFromZhuanchePlace
                        .setSmsType(SmsTemplet.SmsType.arriveFromZhuanchePlace);
                    smsService.insertSmsTemplate("专车单到达预约地", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_arriveFromZhuanchePlace);
                } else {
                    if (!sms_arriveFromZhuanchePlace.getContent()
                        .equals(smsTempletContent.getArriveFromZhuanchePlace())) {
                        sms_arriveFromZhuanchePlace
                            .setContent(smsTempletContent.getArriveFromZhuanchePlace());
                        smsService.update(sms_arriveFromZhuanchePlace);
                    }
                }

                SmsTemplet sms_finishZhuancheOrder = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.finishZhuancheOrder);
                if (null == sms_finishZhuancheOrder) {
                    sms_finishZhuancheOrder = new SmsTemplet();
                    sms_finishZhuancheOrder.setCompanyId(companyId);
                    sms_finishZhuancheOrder.setContent(smsTempletContent.getFinishZhuancheOrder());
                    sms_finishZhuancheOrder.setSmsType(SmsTemplet.SmsType.finishZhuancheOrder);
                    smsService.insertSmsTemplate("专车单完成", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_finishZhuancheOrder);
                } else {
                    if (!sms_finishZhuancheOrder.getContent()
                        .equals(smsTempletContent.getFinishZhuancheOrder())) {
                        sms_finishZhuancheOrder
                            .setContent(smsTempletContent.getFinishZhuancheOrder());
                        smsService.update(sms_finishZhuancheOrder);
                    }
                }

                SmsTemplet sms_modifyZhuancheCash = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.modifyZhuancheCash);
                if (null == sms_modifyZhuancheCash) {
                    sms_modifyZhuancheCash = new SmsTemplet();
                    sms_modifyZhuancheCash.setCompanyId(companyId);
                    sms_modifyZhuancheCash.setContent(smsTempletContent.getModifyZhuancheCash());
                    sms_modifyZhuancheCash.setSmsType(SmsTemplet.SmsType.modifyZhuancheCash);
                    smsService.insertSmsTemplate("专车单修改订单金额", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_modifyZhuancheCash);
                } else {
                    if (!sms_modifyZhuancheCash.getContent()
                        .equals(smsTempletContent.getModifyZhuancheCash())) {
                        sms_modifyZhuancheCash
                            .setContent(smsTempletContent.getModifyZhuancheCash());
                        smsService.update(sms_modifyZhuancheCash);
                    }
                }

                SmsTemplet sms_yupaiZhuancheOrder = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.yupaiZhuancheOrder);
                if (null == sms_yupaiZhuancheOrder) {
                    sms_yupaiZhuancheOrder = new SmsTemplet();
                    sms_yupaiZhuancheOrder.setCompanyId(companyId);
                    sms_yupaiZhuancheOrder.setContent(smsTempletContent.getYupaiZhuancheOrder());
                    sms_yupaiZhuancheOrder.setSmsType(SmsTemplet.SmsType.yupaiZhuancheOrder);
                    smsService.insertSmsTemplate("专车单预派单", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_yupaiZhuancheOrder);
                } else {
                    if (!sms_yupaiZhuancheOrder.getContent()
                        .equals(smsTempletContent.getYupaiZhuancheOrder())) {
                        sms_yupaiZhuancheOrder
                            .setContent(smsTempletContent.getYupaiZhuancheOrder());
                        smsService.update(sms_yupaiZhuancheOrder);
                    }
                }

                //货运相关
                SmsTemplet sms_paiFreightOrder = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.paiFreightOrder);
                if (null == sms_paiFreightOrder) {
                    sms_paiFreightOrder = new SmsTemplet();
                    sms_paiFreightOrder.setCompanyId(companyId);
                    sms_paiFreightOrder.setContent(smsTempletContent.getPaiFreightOrder());
                    sms_paiFreightOrder.setSmsType(SmsTemplet.SmsType.paiFreightOrder);
                    smsService.insertSmsTemplate("派给服务人员新工单", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_paiFreightOrder);
                } else {
                    if (!sms_paiFreightOrder.getContent()
                        .equals(smsTempletContent.getPaiFreightOrder())) {
                        sms_paiFreightOrder.setContent(smsTempletContent.getPaiFreightOrder());
                        smsService.update(sms_paiFreightOrder);
                    }
                }

                SmsTemplet sms_acceptFreightOrder = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.acceptFreightOrder);
                if (null == sms_acceptFreightOrder) {
                    sms_acceptFreightOrder = new SmsTemplet();
                    sms_acceptFreightOrder.setCompanyId(companyId);
                    sms_acceptFreightOrder.setContent(smsTempletContent.getAcceptFreightOrder());
                    sms_acceptFreightOrder.setSmsType(SmsTemplet.SmsType.acceptFreightOrder);
                    smsService.insertSmsTemplate("服务人员接单（发给客户）", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_acceptFreightOrder);
                } else {
                    if (!sms_acceptFreightOrder.getContent()
                        .equals(smsTempletContent.getAcceptFreightOrder())) {
                        sms_acceptFreightOrder
                            .setContent(smsTempletContent.getAcceptFreightOrder());
                        smsService.update(sms_acceptFreightOrder);
                    }
                }

                SmsTemplet sms_cancelFreightOrder = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.cancelFreightOrder);
                if (null == sms_cancelFreightOrder) {
                    sms_cancelFreightOrder = new SmsTemplet();
                    sms_cancelFreightOrder.setCompanyId(companyId);
                    sms_cancelFreightOrder.setContent(smsTempletContent.getCancelFreightOrder());
                    sms_cancelFreightOrder.setSmsType(SmsTemplet.SmsType.cancelFreightOrder);
                    smsService.insertSmsTemplate("服务人员销单（发给客户）", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_cancelFreightOrder);
                } else {
                    if (!sms_cancelFreightOrder.getContent()
                        .equals(smsTempletContent.getCancelFreightOrder())) {
                        sms_cancelFreightOrder
                            .setContent(smsTempletContent.getCancelFreightOrder());
                        smsService.update(sms_cancelFreightOrder);
                    }
                }

                SmsTemplet sms_revokeFreightOrder = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.revokeFreightOrder);
                if (null == sms_revokeFreightOrder) {
                    sms_revokeFreightOrder = new SmsTemplet();
                    sms_revokeFreightOrder.setCompanyId(companyId);
                    sms_revokeFreightOrder.setContent(smsTempletContent.getRevokeFreightOrder());
                    sms_revokeFreightOrder.setSmsType(SmsTemplet.SmsType.revokeFreightOrder);
                    smsService.insertSmsTemplate("货运单收回", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_revokeFreightOrder);
                } else {
                    if (!sms_revokeFreightOrder.getContent()
                        .equals(smsTempletContent.getRevokeFreightOrder())) {
                        sms_revokeFreightOrder
                            .setContent(smsTempletContent.getRevokeFreightOrder());
                        smsService.update(sms_revokeFreightOrder);
                    }
                }

                SmsTemplet sms_arriveFromFreightPlace = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.arriveFromFreightPlace);
                if (null == sms_arriveFromFreightPlace) {
                    sms_arriveFromFreightPlace = new SmsTemplet();
                    sms_arriveFromFreightPlace.setCompanyId(companyId);
                    sms_arriveFromFreightPlace
                        .setContent(smsTempletContent.getArriveFromFreightPlace());
                    sms_arriveFromFreightPlace
                        .setSmsType(SmsTemplet.SmsType.arriveFromFreightPlace);
                    smsService.insertSmsTemplate("货运单到达预约地", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_arriveFromFreightPlace);
                } else {
                    if (!sms_arriveFromFreightPlace.getContent()
                        .equals(smsTempletContent.getArriveFromFreightPlace())) {
                        sms_arriveFromFreightPlace
                            .setContent(smsTempletContent.getArriveFromFreightPlace());
                        smsService.update(sms_arriveFromFreightPlace);
                    }
                }

                SmsTemplet sms_finishFreightOrder = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.finishFreightOrder);
                if (null == sms_finishFreightOrder) {
                    sms_finishFreightOrder = new SmsTemplet();
                    sms_finishFreightOrder.setCompanyId(companyId);
                    sms_finishFreightOrder.setContent(smsTempletContent.getFinishFreightOrder());
                    sms_finishFreightOrder.setSmsType(SmsTemplet.SmsType.finishFreightOrder);
                    smsService.insertSmsTemplate("货运单完成", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_finishFreightOrder);
                } else {
                    if (!sms_finishFreightOrder.getContent()
                        .equals(smsTempletContent.getFinishFreightOrder())) {
                        sms_finishFreightOrder
                            .setContent(smsTempletContent.getFinishFreightOrder());
                        smsService.update(sms_finishFreightOrder);
                    }
                }

                SmsTemplet sms_modifyFreightCash = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.modifyFreightCash);
                if (null == sms_modifyFreightCash) {
                    sms_modifyFreightCash = new SmsTemplet();
                    sms_modifyFreightCash.setCompanyId(companyId);
                    sms_modifyFreightCash.setContent(smsTempletContent.getModifyFreightCash());
                    sms_modifyFreightCash.setSmsType(SmsTemplet.SmsType.modifyFreightCash);
                    smsService.insertSmsTemplate("货运单修改订单金额", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_modifyFreightCash);
                } else {
                    if (!sms_modifyFreightCash.getContent()
                        .equals(smsTempletContent.getModifyFreightCash())) {
                        sms_modifyFreightCash.setContent(smsTempletContent.getModifyFreightCash());
                        smsService.update(sms_modifyFreightCash);
                    }
                }

                SmsTemplet sms_yupaiFreightOrder = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.yupaiFreightOrder);
                if (null == sms_yupaiFreightOrder) {
                    sms_yupaiFreightOrder = new SmsTemplet();
                    sms_yupaiFreightOrder.setCompanyId(companyId);
                    sms_yupaiFreightOrder.setContent(smsTempletContent.getYupaiFreightOrder());
                    sms_yupaiFreightOrder.setSmsType(SmsTemplet.SmsType.yupaiFreightOrder);
                    smsService.insertSmsTemplate("货运单预派单", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_yupaiFreightOrder);
                } else {
                    if (!sms_yupaiFreightOrder.getContent()
                        .equals(smsTempletContent.getYupaiFreightOrder())) {
                        sms_yupaiFreightOrder.setContent(smsTempletContent.getYupaiFreightOrder());
                        smsService.update(sms_yupaiFreightOrder);
                    }
                }

                //城际专线相关
                SmsTemplet sms_paiZhuanxianOrder = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.paiZhuanxianOrder);
                if (null == sms_paiZhuanxianOrder) {
                    sms_paiZhuanxianOrder = new SmsTemplet();
                    sms_paiZhuanxianOrder.setCompanyId(companyId);
                    sms_paiZhuanxianOrder.setContent(smsTempletContent.getPaiZhuanxianOrder());
                    sms_paiZhuanxianOrder.setSmsType(SmsTemplet.SmsType.paiZhuanxianOrder);
                    smsService.insertSmsTemplate("派给服务人员新工单", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_paiZhuanxianOrder);
                } else {
                    if (!sms_paiZhuanxianOrder.getContent()
                        .equals(smsTempletContent.getPaiZhuanxianOrder())) {
                        sms_paiZhuanxianOrder.setContent(smsTempletContent.getPaiZhuanxianOrder());
                        smsService.update(sms_paiZhuanxianOrder);
                    }
                }

                SmsTemplet sms_acceptZhuanxianOrder = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.acceptZhuanxianOrder);
                if (null == sms_acceptZhuanxianOrder) {
                    sms_acceptZhuanxianOrder = new SmsTemplet();
                    sms_acceptZhuanxianOrder.setCompanyId(companyId);
                    sms_acceptZhuanxianOrder
                        .setContent(smsTempletContent.getAcceptZhuanxianOrder());
                    sms_acceptZhuanxianOrder.setSmsType(SmsTemplet.SmsType.acceptZhuanxianOrder);
                    smsService.insertSmsTemplate("服务人员接单（发给客户）", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_acceptZhuanxianOrder);
                } else {
                    if (!sms_acceptZhuanxianOrder.getContent()
                        .equals(smsTempletContent.getAcceptZhuanxianOrder())) {
                        sms_acceptZhuanxianOrder
                            .setContent(smsTempletContent.getAcceptZhuanxianOrder());
                        smsService.update(sms_acceptZhuanxianOrder);
                    }
                }

                SmsTemplet sms_cancelZhuanxianOrder = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.cancelZhuanxianOrder);
                if (null == sms_cancelZhuanxianOrder) {
                    sms_cancelZhuanxianOrder = new SmsTemplet();
                    sms_cancelZhuanxianOrder.setCompanyId(companyId);
                    sms_cancelZhuanxianOrder
                        .setContent(smsTempletContent.getCancelZhuanxianOrder());
                    sms_cancelZhuanxianOrder.setSmsType(SmsTemplet.SmsType.cancelZhuanxianOrder);
                    smsService.insertSmsTemplate("服务人员销单（发给客户）", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_cancelZhuanxianOrder);
                } else {
                    if (!sms_cancelZhuanxianOrder.getContent()
                        .equals(smsTempletContent.getCancelZhuanxianOrder())) {
                        sms_cancelZhuanxianOrder
                            .setContent(smsTempletContent.getCancelZhuanxianOrder());
                        smsService.update(sms_cancelZhuanxianOrder);
                    }
                }

                SmsTemplet sms_revokeZhuanxianOrder = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.revokeZhuanxianOrder);
                if (null == sms_revokeZhuanxianOrder) {
                    sms_revokeZhuanxianOrder = new SmsTemplet();
                    sms_revokeZhuanxianOrder.setCompanyId(companyId);
                    sms_revokeZhuanxianOrder
                        .setContent(smsTempletContent.getRevokeZhuanxianOrder());
                    sms_revokeZhuanxianOrder.setSmsType(SmsTemplet.SmsType.revokeZhuanxianOrder);
                    smsService.insertSmsTemplate("城际专线单收回", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_revokeZhuanxianOrder);
                } else {
                    if (!sms_revokeZhuanxianOrder.getContent()
                        .equals(smsTempletContent.getRevokeZhuanxianOrder())) {
                        sms_revokeZhuanxianOrder
                            .setContent(smsTempletContent.getRevokeZhuanxianOrder());
                        smsService.update(sms_revokeZhuanxianOrder);
                    }
                }

                SmsTemplet sms_arriveFromZhuanxianPlace = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.arriveFromZhuanxianPlace);
                if (null == sms_arriveFromZhuanxianPlace) {
                    sms_arriveFromZhuanxianPlace = new SmsTemplet();
                    sms_arriveFromZhuanxianPlace.setCompanyId(companyId);
                    sms_arriveFromZhuanxianPlace
                        .setContent(smsTempletContent.getArriveFromZhuanxianPlace());
                    sms_arriveFromZhuanxianPlace
                        .setSmsType(SmsTemplet.SmsType.arriveFromZhuanxianPlace);
                    smsService.insertSmsTemplate("城际专线单到达预约地", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_arriveFromZhuanxianPlace);
                } else {
                    if (!sms_arriveFromZhuanxianPlace.getContent()
                        .equals(smsTempletContent.getArriveFromZhuanxianPlace())) {
                        sms_arriveFromZhuanxianPlace
                            .setContent(smsTempletContent.getArriveFromZhuanxianPlace());
                        smsService.update(sms_arriveFromZhuanxianPlace);
                    }
                }

                SmsTemplet sms_finishZhuanxianOrder = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.finishZhuanxianOrder);
                if (null == sms_finishZhuanxianOrder) {
                    sms_finishZhuanxianOrder = new SmsTemplet();
                    sms_finishZhuanxianOrder.setCompanyId(companyId);
                    sms_finishZhuanxianOrder
                        .setContent(smsTempletContent.getFinishZhuanxianOrder());
                    sms_finishZhuanxianOrder.setSmsType(SmsTemplet.SmsType.finishZhuanxianOrder);
                    smsService.insertSmsTemplate("城际专线单完成", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_finishZhuanxianOrder);
                } else {
                    if (!sms_finishZhuanxianOrder.getContent()
                        .equals(smsTempletContent.getFinishZhuanxianOrder())) {
                        sms_finishZhuanxianOrder
                            .setContent(smsTempletContent.getFinishZhuanxianOrder());
                        smsService.update(sms_finishZhuanxianOrder);
                    }
                }

                SmsTemplet sms_modifyZhuanxianCash = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.modifyZhuanxianCash);
                if (null == sms_modifyZhuanxianCash) {
                    sms_modifyZhuanxianCash = new SmsTemplet();
                    sms_modifyZhuanxianCash.setCompanyId(companyId);
                    sms_modifyZhuanxianCash.setContent(smsTempletContent.getModifyZhuanxianCash());
                    sms_modifyZhuanxianCash.setSmsType(SmsTemplet.SmsType.modifyZhuanxianCash);
                    smsService.insertSmsTemplate("城际专线单修改订单金额", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_modifyZhuanxianCash);
                } else {
                    if (!sms_modifyZhuanxianCash.getContent()
                        .equals(smsTempletContent.getModifyZhuanxianCash())) {
                        sms_modifyZhuanxianCash
                            .setContent(smsTempletContent.getModifyZhuanxianCash());
                        smsService.update(sms_modifyZhuanxianCash);
                    }
                }

                SmsTemplet sms_yupaiZhuanxianOrder = smsService
                    .findByAlias(companyId, SmsTemplet.SmsType.yupaiZhuanxianOrder);
                if (null == sms_yupaiZhuanxianOrder) {
                    sms_yupaiZhuanxianOrder = new SmsTemplet();
                    sms_yupaiZhuanxianOrder.setCompanyId(companyId);
                    sms_yupaiZhuanxianOrder.setContent(smsTempletContent.getYupaiZhuanxianOrder());
                    sms_yupaiZhuanxianOrder.setSmsType(SmsTemplet.SmsType.yupaiZhuanxianOrder);
                    smsService.insertSmsTemplate("城际专线单预派单", SmsTemplet.SMSTEMPLATE_TYPE_NOTICE,
                        sms_yupaiZhuanxianOrder);
                } else {
                    if (!sms_yupaiZhuanxianOrder.getContent()
                        .equals(smsTempletContent.getYupaiZhuanxianOrder())) {
                        sms_yupaiZhuanxianOrder
                            .setContent(smsTempletContent.getYupaiZhuanxianOrder());
                        smsService.update(sms_yupaiZhuanxianOrder);
                    }
                }

            }

            String detail = "【%s】修改了短信模板";
            OperationLogEvent operationLogEvent = new OperationLogEvent(principal, detail);
            applicationEventMulticaster.multicastEvent(operationLogEvent);

            return "redirect:/sms/editSms";
        } else {
            throw new NoPermissionException();
        }

    }


    @RequestMapping(value = "recharge")
    public String recharge(Model model) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("sms")) {
            try {
                Page<Map<String, Object>> page = smsService
                    .queryRechargeRecord(Pager.getPage(), Pager.getLimit());
                model.addAttribute("page", page);

            } catch (Exception e) {
                e.printStackTrace();
            }

            return "sms/recharge";
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "sms/sendpost", method = RequestMethod.POST)
    public @ResponseBody
    Result<String> smsDownload(String phone, String content) {

        Result<String> vo = new Result<String>();
        vo.setSuccess(false);
        if (StringUtils.isNotBlank(content)) {
            try {

                if (StringUtils.isBlank(phone)) {
                    List<String> errors = new ArrayList<String>();

                    List<Driver> drivers = driverService.findAll();

                    for (Driver driver : drivers) {
                        //短信
                        int ret = -99;
                        try {
                            Company driverCompany = companyService.findOne(driver.getCompanyId());
                            Company finalCompany = driverCompany;
                            Company main = companyService.getMain();
                            Setting setting = settingUtils.get();
                            if (!main.getId().equals(driverCompany.getId()) && setting
                                .isSmsMainCompany()) {
                                finalCompany = main;
                            }
                            SmsTemplet templet = smsService
                                .findByAlias(finalCompany.getId(), SmsType.customDriver);
                            if (templet == null) {
                                SmsTemplet smsTemplet = new SmsTemplet();
                                smsTemplet.setSmsType(SmsType.customDriver);
                                smsTemplet.setCompanyId(finalCompany.getId());
                                smsTemplet.setContent("{content}");
                                smsTemplet.setSmsType(SmsTemplet.SmsType.customDriver);
                                smsService.insertSmsTemplate("给服务人员发自定义短信",
                                    SmsTemplet.SMSTEMPLATE_TYPE_CODE, smsTemplet);
                            }
                            if (templet != null && StringUtils.isNotBlank(templet.getContent())) {
                                ret = smsService.sendSms(driver.getPhone(), "customDriver",
                                    "{\"content\":\"" + content + "\"}", finalCompany.getId(), 0,
                                    finalCompany.getSignName());
                            }
                        } catch (Exception e) {
                            errors.add(e.getMessage() + "---ret--" + ret);
                        }
                        Thread.sleep(1000);
                    }

                    vo.setData(errors);
                    vo.setSuccess(true);
                    return vo;
                } else {

                    List<String> errors = new ArrayList<String>();
                    //短信
                    int ret = -99;
                    try {
                        Company mainCompany = companyService.getMain();
                        SmsTemplet templet = smsService
                            .findByAlias(mainCompany.getId(), SmsType.custom);
                        if (templet == null) {
                            SmsTemplet smsTemplet = new SmsTemplet();
                            smsTemplet.setSmsType(SmsType.custom);
                            smsTemplet.setCompanyId(mainCompany.getId());
                            smsTemplet.setContent("{content}");
                            smsService.insertSmsTemplate("自定义短信", SmsTemplet.SMSTEMPLATE_TYPE_CODE,
                                smsTemplet);
                        }
                        if (templet != null && StringUtils.isNotBlank(templet.getContent())) {
                            ret = smsService
                                .sendSms(phone, "custom", "{\"content\":\"" + content + "\"}",
                                    mainCompany.getId(), 0, mainCompany.getSignName());
                        }
                    } catch (Exception e) {
                        errors.add(e.getMessage() + "---ret--" + ret);
                    }

                    vo.setData(errors);
                    vo.setSuccess(true);
                    return vo;

                }

            } catch (Exception e) {
                e.printStackTrace();
                vo.setMessage("内部服务器出错");
                return vo;
            }
        } else {
            vo.setMessage("内容不能为空");
            return vo;
        }

    }


    @RequestMapping(value = "sms/send")
    public String send(Model model) {
        return "common/smssend";
    }

    @RequestMapping(value = "sendMember")
    public String sendMember(Model model) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("membersms")) {
            return "sms/sendmember";
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "sendDriver")
    public String sendDriver(Model model) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("driversms")) {
            return "sms/senddriver";
        } else {
            throw new NoPermissionException();
        }
    }

    private String getBasePath(HttpServletRequest req) {

        try {
            return WebUtils.getRealPath(req.getSession().getServletContext(), "/");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return StringUtils.EMPTY;
    }

    @RequestMapping(value = "/smsTest", method = RequestMethod.GET)
    public int smsTest(String phone) {
        JSONObject smsParam = new JSONObject();
        smsParam.put("code", "123456");
        int code = smsService
            .sendSms(phone, "SMS_175574059", smsParam.toJSONString(), Long.valueOf(1), 0, "e族出行");
        return code;
    }
}
